package com.lankystar.logistucs.spring.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.logistucs.spring.dao.IOneOrderDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderDao;
import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.OneOrder;
import com.lankystar.logistucs.spring.entity.TwoStatus;
import com.lankystar.logistucs.spring.service.OneOrderService;

@Service
public class OneOrderServiceImpl implements OneOrderService {

	@Autowired
	private IOneOrderDao oneOrderDao;
	@Autowired
	private ITwoOrderDao twoOrderDao;
	@Override
	public List<Map<String, Object>> queryOrderAndStatus(String totalOrderNu, String pointOrderNu, int oneStatusKey,int userId) {
		List<Map<String, Object>> maplist = new ArrayList<>();
		List<Object[]> oneOrderlist = oneOrderDao.selectOrderAndStatus(totalOrderNu, pointOrderNu, oneStatusKey);
		for (Object[] obs : oneOrderlist) {
			OneOrder oneOrder = (OneOrder) obs[0];
			Cargo cargo = (Cargo) obs[1];
			Map<String, Object> map = new HashMap<>();
			map.put("oneOrderId", oneOrder.getOneOrderId());
			map.put("oneOrderNu", oneOrder.getOneOrderNu());
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			maplist.add(map);
		}
		return maplist;
	}
	@Override
	public Object[] selectOrderAndStatus(String totalOrderNu, String pointOrderNu, int userId) {
		List<Object[]> ob = oneOrderDao.selectOrderAndStatus(totalOrderNu, pointOrderNu, userId);
		int i =0;
		Object[] obs = new Object[ob.size()];
		for (Object[] obj : ob) {
			Map<String, Object> map = new HashMap<>();
			OneOrder oneOrder = (OneOrder) obj[0];
			Cargo cargo = (Cargo) obj[1];
			int j =0;
			Object[] sob = {};
			if(oneOrder!=null){
				List<TwoStatus> ts = twoOrderDao.selectTwoStatus(oneOrder.getOneOrderId());
				sob = new Object[ts.size()];
				for (TwoStatus status : ts) {
					Map<String, Object> smap = new HashMap<>();
					smap.put("statusKey", status.getTwoStatusKey());
					smap.put("statusValue", status.getTwoStatusValue());
					sob[j] = smap;
					j++;
				}
				map.put("oneOrderId", oneOrder.getOneOrderId());
				map.put("type", 0);
			}else{
				map.put("oneOrderId", "");
				map.put("type", 1);
			}
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			map.put("createTime", cargo.getCreateTime());
			map.put("status", sob);
			obs[i] = map;
			i++;
		}
		return obs;
	}

}
